#include "GLTestApplication.h"
#include "linear_least_squares.h"
#include "math_util.h"

using namespace OpenGLWrapper;

GLTestApplication::GLTestApplication()
{
}

GLTestApplication::~GLTestApplication()
{
}

void GLTestApplication::TestLeastSquares()
{
	srand(GetTickCount());

	for (int i = 0; i < 50; i++)
	{
		_pointsX.push_back(rand() / double(RAND_MAX) * 2); 
		_pointsY.push_back(_pointsX[i] * (rand() / double(RAND_MAX) * 2) + _pointsX[i] * _pointsX[i] * (rand() / double(RAND_MAX) * 2));
		_pointsColors.push_back(Color(1.0, 0.0, 0.0));
	}

	const int degree = 2;
	double parameters[degree + 1];
	double error = linear_least_squares(degree, _pointsX.size(), _pointsX.data(), _pointsY.data(), parameters);
	int sz = _pointsX.size();
	for (int i = 0; i < sz; i++)
	{
		_pointsX.push_back(_pointsX[i]);
		_pointsY.push_back(evaluate_polynomial(_pointsX[i], parameters, degree + 1));
		_pointsColors.push_back(Color(0.0, 1.0, 0.0));
	}
}
